﻿@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <title>Index</title>
    <script src='@Url.Content("~/Scripts/zTree/js/jquery-1.4.4.min.js")' type="text/javascript"></script>
    <link href='@Url.Content("~/Scripts/zTree/css/zTreeStyle/zTreeStyle.css")' rel="stylesheet" type="text/css" />
    <script src='@Url.Content("~/Scripts/zTree/js/jquery.ztree.core-3.5.js")' type="text/javascript"></script>
    <script src='@Url.Content("~/Scripts/zTree/js/jquery.ztree.excheck-3.5.js")' type="text/javascript"></script>
    <script src='@Url.Content("~/Scripts/zTree/js/jquery.ztree.exedit-3.5.js")' type="text/javascript"></script>
    <script src='@Url.Content("~/Scripts/PageCreater/Scripts/PageCreater.js")' type="text/javascript"></script>
    <script src='@Url.Content("~/Scripts/easyvalidator/js/easy_validator.pack.js")' type="text/javascript"></script>
    <script src='@Url.Content("~/Scripts/easyvalidator/js/jquery.bgiframe.min.js")' type="text/javascript"></script>
    <link href='@Url.Content("~/Scripts/easyvalidator/css/validate.css")' rel="stylesheet" type="text/css" />
    <link href='@Url.Content("~/Content/Site.css")' rel="stylesheet" type="text/css" />
    <style type="text/css">
        .ztree li span.button.add {margin-left:2px; margin-right: -1px; background-position:-144px 0; vertical-align:top; *vertical-align:middle}
    </style>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#divDep").hide();
            $.ajaxSetup({ cache: false });
            $.fn.zTree.init($("#Tree"), setting, zNodes);
        })
        var zNodes = [
        ];
        var setting = {
            async: {
                enable: true,
                url: "/Organization/GetAllOrg",
                type: "Get",
                dataType: "json"
            },
            view: {
                addHoverDom: addHoverDom,
                removeHoverDom: removeHoverDom
            },
            edit: {
                enable: true,
                showRemoveBtn: true,
                showRenameBtn: false
            },
            data: {
                keep: {
                    parent: true,
                    leaf: true
                },
                simpleData: {
                    enable: true
                }
            },
            callback: {
                onAsyncSuccess: function (event, treeId, treeNode, msg) {
                    var treeObj = $.fn.zTree.getZTreeObj("Tree");
                    treeObj.setting.async.enable = false;
                },
                onAsyncError: function (event, treeId, treeNode, XMLHttpRequest, textStatus, errorThrown) {
                    alert(errorThrown);
                },
                onClick: function (event, treeId, treeNode) {
                    $("#nodeID").val(treeNode.id);

                    $("#Name").val(treeNode.name);
                    $("#sltSit").empty();
                    $("#sltSit").append("<option value='0'>---请选择---</option>");
                    if (treeNode.id != 0) {
                        if (treeNode.tld == "True" || treeNode.tld == "1") {
                            $("#sltSit").append("<option value='1' selected='selected'>激活</option>");
                            $("#sltSit").append("<option value='2'>注销</option>");
                        } else if (treeNode.tld == "False" || treeNode.tld == "2") {
                            $("#sltSit").append("<option value='1'>激活</option>");
                            $("#sltSit").append("<option value='2' selected='selected'>注销</option>");
                        } else {
                            $("#sltSit").append("<option value='1'>激活</option>");
                            $("#sltSit").append("<option value='2'>注销</option>");
                        }
                    } else {
                        $("#sltSit").append("<option value='1'  selected='selected'>激活</option>");
                        $("#sltSit").append("<option value='2'>注销</option>");
                    }

                    $("#divDep").hide(400);
                    $("#divDep").show(400);
                },
                beforeRemove: function (treeId, treeNode) {
                    return confirm("确认删除？");
                },
                onRemove: function (event, treeId, treeNode) {
                    if (treeNode.id == 0) {
                        alert("操作成功！");
                    } else {
                        $.ajax({
                            type: "Post",
                            url: "/Organization/Delete",
                            data: { id: treeNode.id },
                            success: function (json) {
                                alert("操作成功！");
                            }
                        })
                    }
                    CleanDetail();
                }
            }
        };
        function addHoverDom(treeId, treeNode) {
            var sObj = $("#" + treeNode.tId + "_span");
            if (treeNode.editNameFlag || $("#addBtn_" + treeNode.id).length > 0) return;
            var addStr = "<span class='button add' id='addBtn_" + treeNode.id
				+ "' title='add node' onfocus='this.blur();'></span>";
            sObj.after(addStr);
            var btn = $("#addBtn_" + treeNode.id);
            if (btn) btn.bind("click", function () {
                var zTree = $.fn.zTree.getZTreeObj("Tree");
                zTree.addNodes(treeNode, { id: 0, pId: treeNode.id, name: "新增的指标，请点击此进行编辑", isParent: true, icon: "../../Content/img/diy/1_close.png" });
                return false;
            });
        };
        function removeHoverDom(treeId, treeNode) {
            $("#addBtn_" + treeNode.id).unbind().remove();
        };
        function addNewNode() {
            var treeObj = $.fn.zTree.getZTreeObj("Tree");
            var newNode = { id: 0, pid: null, name: "新增的指标，请点击此进行编辑", isParent: true, icon: "../../Content/img/diy/1_close.png" };
            newNode = treeObj.addNodes(null, newNode);
        };
        function UpdateNode() {
            var i = InitValidator();
            if (i == false) {
                return false;
            }
            var Obj = $.fn.zTree.getZTreeObj("Tree");
            var node = Obj.getNodeByParam("id", $("#nodeID").val(), null);
            node.name = $("#Name").val();
            node.tld = $("#sltSit").val();
            Obj.updateNode(node);

            $.ajax({
                type: "POST",
                url: "/Organization/Opreate",
                data: { isActive: function () { return $("#sltSit").val() }, id: function () { return $("#nodeID").val() }, name: function () { return $("#Name").val() }, pId: node.pId, level: node.level },
                success: function () {
                    $.get("/Organization/GetEntiyID", { name: function () { return $("#Name").val() } }, function (str) {
                        node.id = str;
                    })
                    Obj.updateNode(node);
                    Obj.refresh();
                    alert("操作成功！");
                },
                error: function (msg) {
                    alert(msg);
                },
                complete: function () {
                    CleanDetail();
                }
            });
        }
        function CleanDetail() {
            $("#divDep").hide(70);
            $("#Name").val();
            $("#sltSit").empty();
            $("#sltSit").append("<option value='0'>---请选择---</option><option value='1'>激活</option><option value='2'>注销</option>");
        };
        function InitValidator() {
            var isSuccess = true;
            var arr = new Array();
            var i = 0;
            $("select,input").each(function () {
                var flag = true;
                var aaa = $(this).attr("id");
                if ($(this).attr("reg") != undefined) {
                    if (!validate($(this))) {
                        flag = false;
                    }
                }
                arr[i] = flag;
                i++;
            });
            for (var j = 0; j < arr.length; j++) {
                if (arr[j] == false) {
                    alert("验证失败，请查看页面提示！");
                    isSuccess = false;
                    break;
                }
            }
            return isSuccess;
        }
    </script>
</head>
<body>
    <a href="javascript:void(0);" onclick="addNewNode()">新增机构</a>
    <div>
        <div style="float: left; width: 250px;">
            <ul id="Tree" class="ztree">
            </ul>
        </div>
        <div id="divDep" style="position: fixed; left: 35%;">
            <table>
                <tr>
                    <td>
                        机构名称
                    </td>
                    <td>
                        <input id="Name" name="Name" type="text" reg=".+" tip="用户名不能为空" />
                    </td>
                </tr>
                <tr>
                    <td>
                        是否激活
                    </td>
                    <td>
                        <select id="sltSit" reg="[^0]" tip="请选择状态">
                            <option value="0">---请选择---</option>
                            <option value="1">激活</option>
                            <option value="2">注销</option>
                        </select>
                    </td>
                </tr>
            </table>
            <div>
                <input id="btnSure" type="button" value="确定" class="btn1" onclick="UpdateNode()" />
            </div>
        </div>
    </div>
    <input type="hidden" id="nodeID" />
</body>
</html>
